package com.wlcj.www.web.data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jodd.datetime.JDateTime;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import com.wlcj.constant.Global;
import com.wlcj.util.RedisUtil;
import com.wlcj.www.enumtype.DataStoreType;
import com.wlcj.www.enumtype.IndustryBaoBiaoEnum;
import com.wlcj.www.enumtype.PlatformDataUnit;
import com.wlcj.www.model.data.Data;
import com.wlcj.www.model.data.DateIndustry;
import com.wlcj.www.model.data.PlatformAttention;
import com.wlcj.www.model.data.PlatformQuestion;
import com.wlcj.www.model.data.Province;
import com.wlcj.www.model.data.ProvinceIndustry;
import com.wlcj.www.model.platform.Platform;
import com.wlcj.www.service.AttentionService;
import com.wlcj.www.service.DataService;
import com.wlcj.www.util.DataUtil;
import com.wlcj.www.util.EchartsUtils;
import com.wlcj.www.web.BaseAction;

public class DataAction extends BaseAction {

	private DataService dataService = s(DataService.class);
	private AttentionService dataService1 = s(AttentionService.class);
	private AttentionService attentionService = s(AttentionService.class);
	// 平台数据
	public void platform() {
		List<Data> data = Data.dao.getMaxDate();
		/*String time = data.get(0).getDate("data_date").toString();
		String provinceId = getPara("provinceId");
		List<Data> dataList = dataService.getDataList(time + "/" + time,
				"volume desc", provinceId);*/
		/**
		 * 平台数据二期改版优化
		 */
		// 1.当月投资人数和累计代还

		JDateTime jdt = new JDateTime(data.get(0).getDate("data_date"));
		jdt.setDay(1);
		String investTime = jdt.toString("YYYY-MM-DD");
		List<Data> investmentInterest = investmentInterestDetile(dataService
				.getInvestmentInterest(investTime));
		// 4.投资期限分布
		List<Data> investmentPeriod = investmentPeriodDetile(dataService
				.getInvestmentPeriod(investTime));
		// 5.当月行业每日成交量走势情况
		jdt.addMonth(-1);
		jdt.setDay(1);
		//setAttr("dataList", dataList);
		// 广告位
		/*Record ad = indexService.getChannelAd(Global.DATA_CHANNEL_AD_ID);
		setAttr("ad", ad);*/
		setAttr("investmentInterest", investmentInterest.get(0));// 投资利率分布
		setAttr("investmentPeriod", investmentPeriod.get(0)); // 投资期限分布
		// //上月投资期限分布图
	//	setAttr("time", time);
		setAttr("month",  new JDateTime(data.get(0).getDate("data_date")).toString("YYYY-MM-DD"));
		renderJsp("newplatform.jsp");
	}

	/**
	 * 累计投资的ajax请求
	 */
	public void totalInvestment() {
		List<Data> data = Data.dao.getMaxDate();// 数据库中最大的一天
		JDateTime jdt = new JDateTime(data.get(0).getDate("data_date"));
		jdt.setDay(1);
		String investTime = jdt.toString("YYYY-MM-DD");
		List<Data> investList = dataService.getInvestPeople(investTime);// 累计投资
		DecimalFormat myformart = new DecimalFormat("0.00");
		List<Data> stillList = dataService.getDayStill(new JDateTime(data
				.get(0).getDate("data_date")).toString());
		setAttr("stillTime",
				stillList.get(0).get("cumulative_amount") != null ? myformart
						.format(((BigDecimal) (stillList.get(0)
								.get("cumulative_amount")))
								.divide(new BigDecimal("100"))) : "暂无数据");// 累计代还
		setAttr("investment",
				investList.get(0).get("investment_num") != null ? (investList
						.get(0).get("investment_num")) : "暂无数据");// 投资人数SUM
		setAttr("volume",
				investList.get(0).get("volume") != null ? myformart
						.format(((BigDecimal) investList.get(0).get("volume"))
								.divide(new BigDecimal("100"))) : "暂无数据");// 累计投资SUM
		renderJson();

	}

	/**
	 * 未来网贷当月投资金额监控ajax请求
	 */
	public void amountOfInvestmentControl() {
		List<Data> data = Data.dao.getMaxDate();// 数据库中最大的一天
		JDateTime jdt = new JDateTime(data.get(0).getDate("data_date"));
		jdt.getDayOfMonth();
		String investTime = jdt.toString("YYYY-MM-DD");
		String invest2Time = jdt.addDay(-30).toString("YYYY-MM-DD");
		String centerMonth = jdt.addDay(30).addMonth(-1).toString("YYYY-MM-DD");
		String lastMonth = jdt.addDay(-30).toString("YYYY-MM-DD");
		/**
		 * 最近三十日数据对比的横坐标处理
		 */
		List<String> xzhouOne = new ArrayList<String>();//上个月
		List<String> xzhouTwo = new ArrayList<String>();//本月
		jdt.addDay(30);
		Map<String, List<Data>> nowDataPKlastMonth = deilData(dataService
				.getYesterdayDataPkDayData(investTime,invest2Time,centerMonth,lastMonth ));
		
		for(int i=0;i<30;i++){
			xzhouOne.add(jdt.addDay(1).toString("DD"));//
			
		}
		for(int i=0;i<30;i++){
			xzhouTwo.add(jdt.addDay(1).toString("DD"));
			
		}
         if(xzhouOne.contains("31")){
        	 setAttr("xzhou", nowDataPKlastMonth.get("nowMonth"));
        	 setAttr("lastOrMonth", "now");
         }else{
        	 setAttr("xzhou", nowDataPKlastMonth.get("lastMonth"));
        	 setAttr("lastOrMonth", "last");
        }
		//xzhouOne.contains("31")?setAttr("xzhou", xzhouOne):setAttr("xzhou", xzhouTwo);
         jdt=new JDateTime(data.get(0).getDate("data_date"));
         setAttr("nowTime", new JDateTime(data.get(0).getDate("data_date")).toString("MM")+"月");
         setAttr("lastTime", new JDateTime(data.get(0).getDate("data_date")).addMonth(-1).toString("MM")+"月");
		setAttr("nowMonth", test(nowDataPKlastMonth.get("nowMonth"),jdt.getDay(),jdt.getMonthLength(jdt.getMonth()-1)));// 当月投资期限分布图
		setAttr("lastMonth", test(nowDataPKlastMonth.get("lastMonth"),jdt.getDay(),jdt.getMonthLength(jdt.getMonth()-2)));// 上月投资期限分布图
		renderJsp("amountOfInvestmentControl.jsp");
	}
	
	
	public static final int max = 31;
	
	public List<Data> test(List<Data> param, int day , int count){
		List<Data> paramnew = new ArrayList<Data>();
		List<Data> paramnew2 = new ArrayList<Data>();
		for(int i = 0 ; i <count ; i ++){
			if(day - i==0){
				day = max+day;
				for(int j = max ; j >count;j--){
					Data tempData = new Data();
					tempData.put("data_date_copy", j+"号");
					tempData.put("increaseof", "0");
					tempData.put("comparative", "0");
					tempData.put("volume", "0");
					paramnew.add(tempData);
				}
			}
			if(paramnew.size()<=31){
				paramnew.add(param.get(max-i-1));
			}
		}
		for(int i = 0 ; i <  paramnew.size() ;  i++){
			paramnew2.add(paramnew.get(paramnew.size()-i-1));
		}
		return paramnew2;
	}

	public void platformTable() {
		String provinceId = getPara("provinceId");
		String timeStr = getPara("timeStr");// 2014-4-24
		List<Data> dataList = null;
		String[] times = new String[2];

		if (StringUtils.isNotBlank(timeStr)) {
			times = timeStr.split("/");
		} else {
			timeStr = DataUtil.createTypeStr(getParaToInt("timeType"));// 7日，30日，
		}

		String orderby = getPara("orderby");
		String column = getPara("column");
		dataList = dataService.getDataList(timeStr, column + " " + orderby,
				provinceId);
		if (CollectionUtils.isEmpty(dataList)) {
			// todo
			String[] strs = timeStr.split("/");
			if (times[0] != null && times[1] != null) {
				dataList = dataService.getDataList(times, column + " "
						+ orderby, provinceId);
			} else {
				dataList = dataService.getDataList(strs,
						column + " " + orderby, provinceId);
			}
		}
		setAttr("dataList", dataList);
		renderJsp("platformTable.jsp");
	}

	public void addAttention() {
		try {
			if (getSessionAttr(Global.SESSION_LOGIN_KEY) == null) {
				// renderJsp(Global.LOGIN_URL);
				fail("nologin");
			} else {
				Integer[] pid = getParaValuesToInt("pid[]"); // 平台ID
				int uid = getUserId();
				//int pid = getParaToInt("pid");
				for(int i=0;i<pid.length;i++){
					dataService.addUserAttentionPlatform(uid, pid[i]);
				}
				success("ok");
			}
		} catch (Exception e) {
			e.printStackTrace();
			fail("关注失败！！");
		}
	}

	// 行业数据
/*	public void industry() {
		JDateTime jdt = new JDateTime();
		jdt.addMonth(-1);
		jdt.setDay(1);
		String startTime = jdt.toString("YYYY-MM-DD");
		jdt.addMonth(1);
		jdt.setDay(1);
		String endTime = jdt.toString("YYYY-MM-DD");
		List<ProvinceIndustry> provinceIndustryList = dataService
				.getProvinceIndustry(startTime, endTime, null);
		List<DateIndustry> dateIndustryList = dataService.getDateIndustry();
		setAttr("provinceIndustryList", provinceIndustryList);
		setAttr("dateIndustryList", dateIndustryList);
		setAttr("date", new JDateTime());
		renderJsp("industry.jsp");
	}*/

	// 行业数据 请求报表
	public void industryBaoBiao() {
		String orderBy = getPara("orderBy");// 数据报表展示的字段
		Integer year = Integer.parseInt(getPara("year").trim());
		Integer month = Integer.parseInt(getPara("month").trim());
		JDateTime jdt = new JDateTime(year, month, 1);
		String startTime = jdt.toString("YYYY-MM-DD");
		JDateTime stopt = new JDateTime(year, month + 1, 1);
		String stopTime = stopt.toString("YYYY-MM-DD");
		String code = IndustryBaoBiaoEnum.getEnumByName(orderBy).getCode();//
		String codeTwo = IndustryBaoBiaoEnum.getEnumByName(orderBy)
				.getCodeTwo();
		String danwei = IndustryBaoBiaoEnum.getEnumByName(orderBy).getDanwei();
		List<ProvinceIndustry> industry = dataService.getProvinceOrderby(code,
				codeTwo, startTime, stopTime);
		for (int i = 0; i < industry.size(); i++) {
			String name = Province.dao.getCacheProvinceById(
					industry.get(i).getInt("province_id"))
					.get("provinces_name");
			industry.get(i).put("provinces_name", name.replace("省", ""));
			if ("month_borrow_num".equals(code)
					|| "month_investment_num".equals(code)) {
				industry.get(i)
						.put("count",
								Double.parseDouble(industry.get(i).get(code)
										.toString()) / 10000);
			} else {
				industry.get(i).put("count", industry.get(i).get(code));
			}
			if (!"".equals(codeTwo)) {
				industry.get(i).put("codeTwo", industry.get(i).get(codeTwo));
			}
		}
		setAttr("danwei", danwei);
		setAttr("province", industry);
		setAttr("orderBy", orderBy);
		renderJsp("hangye.jsp");
	}

	public void industryProvinceTable() {
		int year = getParaToInt("year");
		int month = getParaToInt("month");

		JDateTime jdt = new JDateTime();
		jdt.setYear(year);
		jdt.setMonth(month);
		jdt.setDay(1);
		String startTime = jdt.toString("YYYY-MM-DD");
		jdt.addMonth(1);
		jdt.setDay(1);
		String endTime = jdt.toString("YYYY-MM-DD");

		String orderby = getPara("orderby");
		String column = getPara("column");
		orderby = column + " " + orderby;
		List<ProvinceIndustry> provinceIndustryList = dataService
				.getProvinceIndustry(startTime, endTime, orderby);
		setAttr("provinceIndustryList", provinceIndustryList);
		renderJsp("industryProvinceTable.jsp");
	}

	// 我的关注
	// public void attention() {
	//
	// }

	// 问题平台
	/*public void question() {
		List<Province> provinceList = dataService.getAllProvinces();
		setAttr("provinceList", provinceList);
		renderJsp("question.jsp");
	}*/

	// 问题平台
	public void questionTable() {
		Map<String, Object> entity = new HashMap<String, Object>();
		// 排序
		entity.put("orderby", getPara("orderby") != null ? getPara("orderby")
				: "desc");
		entity.put("column", getPara("column") != null ? getPara("column")
				: "problem_time");
		// 筛选条件
		// 时间-地区-事件类型
		entity.put("time", getPara("time") != null ? getPara("time") : "问题时间");
		entity.put("area", getPara("area") != null ? getPara("area") : "地区");
		entity.put("type", getPara("type") != null ? getPara("type") : "事件类型");
		List<PlatformQuestion> dataList = dataService.getQuestion(entity);
		setAttr("dataList", dataList);
		renderJsp("questionTable.jsp");
	}

	/**
	 * 处理echarts对比上月的那个报表
	 * 
	 * @param args
	 */
	public Map<String, List<Data>> deilData(Map<String, List<Data>> maps) {
		List<Data> lastmap = maps.get("lastMonth");// 上个月数据
		List<Data> nowmap = maps.get("nowMonth");// 本月数据
		 DecimalFormat myformart = new DecimalFormat("0.000");
		Integer countTb=0;
		if (nowmap.size() > 0) {
			/**
			 * onth=[com.wlcj.www.model.data.Data@eb79ea8a
			 * {volume:118544.060000, data_date:2015-04-01}, 3e8454
			 * {volume:109925.260000, data_date:2015-03-31}]} 同比增长逻辑
			 * （今日-上月今日）/上月今日
			 * 
			 */
			for (int i = 0; i < nowmap.size(); i++) {
				BigDecimal volume = nowmap.get(i).get("volume");// 今日成交量
				Date data = nowmap.get(i).get("data_date");// 今日日期
				JDateTime jdt = new JDateTime();
				jdt.setDateTime(data);
				String nowTime = jdt.toString("YYYY-MM-DD");
				jdt.addDay(-1);
				String yeDate = jdt.toString("YYYY-MM-DD");// 昨天
				JDateTime jdta = new JDateTime();
				jdta.setDateTime(data);
				jdta.addMonth(-1);
				String yestTime = jdta.toString("YYYY-MM-DD");// 上个月的今日
				JDateTime lastMonthLastDay = new JDateTime();
				lastMonthLastDay.setYear(Integer.parseInt(jdta.toString("YYYY")));
				lastMonthLastDay.setMonth(Integer.parseInt(jdta.toString("MM")));
				lastMonthLastDay.setDay(jdta.getMonthLength());
				
				if(yestTime.equals(lastMonthLastDay.toString("YYYY-MM-DD"))){
					countTb++;
				}else{
					countTb=0;
				}
				BigDecimal yeVolume = new BigDecimal(0);// 昨日成交量
				BigDecimal lastMonthVolume = new BigDecimal(0);// 上月今日成交量
				if (nowTime.contains(yeDate.substring(5, 7))) {// 在同一个月
					for (int j = 0; j < nowmap.size(); j++) {
						if (yeDate.equals(nowmap.get(j).get("data_date")
								.toString()))
							yeVolume = nowmap.get(j).get("volume");
					}
				} else {// 不在同一个月
					Map<String, BigDecimal> yeVolumemaps = getincreOrcm(yeDate,
							"", lastmap);
					if (yeVolumemaps.get("yeVolume") != null) {
						yeVolume = yeVolumemaps.get("yeVolume");
					}
				}
				Map<String, BigDecimal> lastMonthVolumemaps = getincreOrcm("",
						yestTime, lastmap);

				if (lastMonthVolumemaps.get("lastMonthVolume") != null) {
					lastMonthVolume = lastMonthVolumemaps
							.get("lastMonthVolume");
				}
				;
				if (yeVolume.compareTo(new BigDecimal(0)) != 0) {
					nowmap.get(i).put("comparative",
							myformart.format(volume.subtract(yeVolume).divide(yeVolume, 2)));// 环比增长
					lastmap.get(i).put("comparative",
							myformart.format(volume.subtract(yeVolume).divide(yeVolume, 2)));// 环比增长
					lastmap.get(i).put("nowMonth",new JDateTime(lastmap.get(i).get("data_date").toString()).toString("MM"));// shangyue月份
					nowmap.get(i).put("nowMonth",new JDateTime(nowmap.get(i).get("data_date").toString()).toString("MM"));// shangyue月份
				} else {
					nowmap.get(i).put("comparative", "0");
					lastmap.get(i).put("comparative", "0");
					lastmap.get(i).put("nowMonth",new JDateTime(lastmap.get(i).get("data_date").toString()).toString("MM"));// 月份
					nowmap.get(i).put("nowMonth",new JDateTime(nowmap.get(i).get("data_date").toString()).toString("MM"));// 月份
				}
				if (lastMonthVolume.compareTo(new BigDecimal(0)) != 0) {// tong比增长
					nowmap.get(i).put(
							"increaseof",
							countTb==0 ||countTb==1   ?myformart.format(volume.subtract(lastMonthVolume).divide(
									lastMonthVolume, 2)):0);
					lastmap.get(i).put(
							"increaseof",
							countTb==0 ||countTb==1   ?myformart.format(volume.subtract(lastMonthVolume).divide(
									lastMonthVolume, 2)):0);
					lastmap.get(i).put("nowMonth",new JDateTime(lastmap.get(i).get("data_date").toString()).toString("MM"));// 月份
					nowmap.get(i).put("nowMonth",new JDateTime(nowmap.get(i).get("data_date").toString()).toString("MM"));// 月份
				} else {
					nowmap.get(i).put("increaseof", "0");
					lastmap.get(i).put("increaseof", "0");
					lastmap.get(i).put("nowMonth",new JDateTime(lastmap.get(i).get("data_date").toString()).toString("MM"));// 月份
					nowmap.get(i).put("nowMonth",new JDateTime(nowmap.get(i).get("data_date").toString()).toString("MM"));// 月份
				}
				Date d = nowmap.get(i).get("data_date");
				JDateTime jdttt = new JDateTime(d);
				
				nowmap.get(i).put("data_date_copy", (jdttt.getDay()<10?0+""+jdttt.getDay():jdttt.getDay()) + "号");
				Date dtwo = lastmap.get(i).get("data_date");
				JDateTime jdtttt = new JDateTime(dtwo);
				lastmap.get(i).put("data_date_copy", (jdtttt.getDay()<10?0+""+jdtttt.getDay():jdtttt.getDay()) + "号");
				
			}
		}
		
		return maps;
	}

	public Map<String, BigDecimal> getincreOrcm(String yeDate, String yestTime,
			List<Data> lastmap) {
		Map<String, BigDecimal> maps = new HashMap<String, BigDecimal>();
		BigDecimal yeVolume = new BigDecimal(0);// 昨日成交量
		BigDecimal lastMonthVolume = new BigDecimal(0);// 上月今日成交量
		for (int j = 0; j < lastmap.size(); j++) {// 环比增长

			if (!"".equals(yeDate)
					&& lastmap.get(j).get("data_date").toString()
							.equals(yeDate)) {
				yeVolume = lastmap.get(j).get("volume");
				maps.put("yeVolume", yeVolume);
			}
			if (!"".equals(yestTime)
					&& lastmap.get(j).get("data_date").toString()
							.equals(yestTime)) {
				lastMonthVolume = lastmap.get(j).get("volume");
				maps.put("lastMonthVolume", lastMonthVolume);
			}

		}
		return maps;
	}

	/**
	 * 投资利率（年化）分布做数据处理
	 * 
	 * @param lists
	 * @return
	 */
	public List<Data> investmentInterestDetile(List<Data> lists) {
		// DecimalFormat myformart = new DecimalFormat("0.00");

		for (int i = 0; i < lists.size(); i++) {

			lists.get(i)
					.put("volumone",
							DataUtil.detileData(lists.get(i).getBigDecimal(
									"volumone")));

			lists.get(i)
					.put("volumetwo",
							DataUtil.detileData(lists.get(i).getBigDecimal(
									"volumetwo")));

			lists.get(i).put(
					"volumethree",
					DataUtil.detileData(lists.get(i).getBigDecimal(
							"volumethree")));

			lists.get(i).put(
					"volumefoue",
					DataUtil.detileData(lists.get(i)
							.getBigDecimal("volumefoue")));

			lists.get(i).put(
					"volumefive",
					DataUtil.detileData(lists.get(i)
							.getBigDecimal("volumefive")));

		}

		return lists;

	}

	/**
	 * 投资期限分布
	 * 
	 * @param lists
	 * @return
	 */
	public List<Data> investmentPeriodDetile(List<Data> lists) {
		// DecimalFormat myformart = new DecimalFormat("0.00");
		for (int i = 0; i < lists.size(); i++) {

			lists.get(i)
					.put("volumeone",
							DataUtil.detileData(lists.get(i).getBigDecimal(
									"volumeone")));

			lists.get(i)
					.put("volumetwo",
							DataUtil.detileData(lists.get(i).getBigDecimal(
									"volumetwo")));

			lists.get(i).put(
					"volumethree",
					DataUtil.detileData(lists.get(i).getBigDecimal(
							"volumethree")));

			lists.get(i).put(
					"volumefour",
					DataUtil.detileData(lists.get(i)
							.getBigDecimal("volumefour")));

			lists.get(i).put(
					"volumefive",
					DataUtil.detileData(lists.get(i)
							.getBigDecimal("volumefive")));

		}
		return lists;
	}

	public void getVolume() {
		List<Data> max = Data.dao.getMaxDate();// 最大时间
		String time = max.get(0).getDate("data_date").toString();
		List<Platform> other = dataService.getVolume(time, getParaToInt("pid"));
		setAttr("other", other);
		renderJsp("other.jsp");
	}

	public static void main(String[] args) {
		JDateTime jdt = new JDateTime("2015-4-9");
		System.out.println(jdt.getMonthLength());;
	}

	public void hangye() {
		renderJsp("hangye.jsp");
	}

	/**
	 * 默认加载每日成交排行
	 */
	public void dayVolume() {
		Map<String, List<Platform>> map = new HashMap<String, List<Platform>>();
		List<Data> max = Data.dao.getMaxDate();// 最大时间
		String time = max.get(0).getDate("data_date").toString();
		List<Platform> beijing = dataService.getVolume(time, 2);
		List<Platform> shanghai = dataService.getVolume(time, 10);
		List<Platform> guangdong = dataService.getVolume(time, 19);
		List<Platform> hubei = dataService.getVolume(time, 17);
		map.put("beijing", beijing);
		map.put("shanghai", shanghai);
		map.put("guangdong", guangdong);
		map.put("hubei", hubei);
		setAttr("map", map);
		setAttr("time", time);
		renderJsp("paihang.jsp");
	}

	/**
	 * 平台数据对比报表
	 */
	public void baobiao() {
		Integer[] platformIdArray = getParaValuesToInt("platformIdArray[]"); // 平台ID
		String orderByVariable = getPara("orderByVariable") == null ? "volume"
				: getPara("orderByVariable"); // 排序字段
		Integer dayOrMonth = getPara("dayOrMonth") == null ? 1
				: getParaToInt("dayOrMonth");// 日报，月报。。
		String dateFormate = dayOrMonth == 1 || dayOrMonth == 2 ? "MM/DD"
				: "YYYY/MM";
		Map<String, List<Object>> dataList = new HashMap<String, List<Object>>();
		List<Data> max = Data.dao.getMaxDate();// 最大时间
		String  platformUnit = PlatformDataUnit.getEnumByName(orderByVariable).getDesc();
		List<String> xzhou = EchartsUtils.getXzhou(
				max.get(0).getDate("data_date"), dayOrMonth);
		String time = max.get(0).getDate("data_date").toString();
		
		List<PlatformAttention> plat = null;
		if (getSessionAttr(Global.SESSION_LOGIN_KEY) == null) {
			dataList = detilePlatform(dataService.topThreeOrOrther(time,
					orderByVariable, platformIdArray, dayOrMonth));
		} else {
			// 关注列表
			int user_id = Integer.parseInt(String
								.valueOf(getSessionAttr(Global.SESSION_LOGIN_KEY)));
			
			Integer[] platId = null;
			if(platformIdArray==null){
				plat = attentionService.platform(user_id,time);
				if(CollectionUtils.isNotEmpty(plat)){
					int size;
					if(plat.size()<5){
						size = plat.size();
					}else{
						size = 5;
					}
					 platId = new Integer[size];
					for(int i=0;i<size;i++){
						platId[i]=plat.get(i).getInt("platform_id");
					}
				}
				platformIdArray = platId;
			}
			
			List<Data> data = attentionService.getAttentionDataList(user_id,time,orderByVariable, platformIdArray, dayOrMonth);
			if (CollectionUtils.isEmpty(data)) {
				dataList = detilePlatform(dataService.topThreeOrOrther(time,
						orderByVariable, platformIdArray, dayOrMonth));
			} else {
				// 关注列表
				dataList = detilePlatform(attentionService
						.getAttentionDataList(user_id,time,orderByVariable, platformIdArray, dayOrMonth));
			}
		}

		List<String> platformId = new ArrayList<String>();
		List<Object> listNameOrId = dataList.get("listNameDetileOrId");
		for (int i = 0; i < listNameOrId.size(); i++) {
			platformId.add(((Data) listNameOrId.get(i)).get("platform_id")
					.toString());
		}
		String data = "";
		/**
		 * 
		 * 对报表数据进行处理
		 */
		for (int i = 0; i < platformId.size(); i++) {
			// setAttr(i+"", dataList.get(platformId.get(i)));
			data += "{ name:'"
					+ ((Data) dataList.get(platformId.get(i)).get(0))
							.get("platform_name") + "', type:'line',symbol:'emptyCircle',symbolSize:4,itemStyle: {normal: {color:'"
					+ EchartsUtils.createClolor(i)+"'"		
				    + "}},data:[";
			for (int j = 0; j < dataList.get(platformId.get(i)).size(); j++) {

				data += ((Data) (dataList.get(platformId.get(i))).get(j)).get(orderByVariable)==null?new BigDecimal("0"):((Data) (dataList.get(platformId.get(i))).get(j)).get(orderByVariable);
				if (j != dataList.get(platformId.get(i)).size() - 1) {
					data += ",";
				}

			}
			data += "]}";
			if (i != platformId.size() - 1) {
				data += ",";
			}

		}
		/**
		 * 对报表数据X轴进行处理
		 */
		String xzhouMessage = "[";
		int i = 0;
		if(dayOrMonth==1){i=0;}else{i=1;}
		for (i=i; i < xzhou.size(); i++) // 外循环是循环的次数
		{
			for (int j = xzhou.size() - 1; j > i; j--) // 内循环是 外循环一次比较的次数
			{
				if (xzhou.get(i).equals(xzhou.get(j))) {
					xzhou.remove(j);
				}

			}
			xzhouMessage += "'"
					+ new JDateTime(xzhou.get(i)).toString(dateFormate) + "'";
			if (i != xzhou.size() - 1) {
				xzhouMessage += ",";
			}
		}
		Collections.sort(xzhou);
		xzhouMessage += "]";
		 setAttr("dayOrMonth", dayOrMonth);// 月还是周
		setAttr("orderByVariable", orderByVariable);//排序字段
		setAttr("unit", platformUnit);
		setAttr("xzhou", xzhouMessage);
		setAttr("dataList", dataList.get("listNameDetile"));
		setAttr("baobiaoText", data);
		if(getSessionAttr(Global.SESSION_LOGIN_KEY) != null){
			if(CollectionUtils.isNotEmpty(plat)){
				setAttr("listNameDetileOrId", plat);
			}else{
				setAttr("listNameDetileOrId", dataList.get("listNameDetileOrId"));
			}
		}else{
			setAttr("listNameDetileOrId", dataList.get("listNameDetileOrId"));
		}
		renderJsp("platDataCompare.jsp");
	}

	/**
	 * 平台数据对比 报表处理
	 */
	/**
	 * 平台数据对比 报表处理
	 */
	public Map<String, List<Object>> detilePlatform(List<Data> lists) {
		Map<String, List<Object>> maps = new HashMap<String, List<Object>>();
		List<Object> listNameDetile = new ArrayList<Object>();
		/*
		 * List<Object> listOne = new ArrayList<Object>(); List<Object> listTwo
		 * = new ArrayList<Object>(); List<Object> listThree = new
		 * ArrayList<Object>();
		 */
		List<Object> listNameDetileOrId = new ArrayList<Object>();
		for (int i = 0; i < lists.size(); i++) {
			if (!listNameDetile.contains(lists.get(i).get("platform_name")
					.toString()) && listNameDetile.size()<=5) {
				listNameDetile
						.add(lists.get(i).get("platform_name").toString());
				listNameDetileOrId.add(lists.get(i));
			}// 对名字的处理
			if (maps.size() <= 0) {
				List<Object> listOne = new ArrayList<Object>();
				listOne.add(lists.get(i));
				maps.put(lists.get(i).get("platform_id").toString(), listOne);
			} else {
				if (maps.get(lists.get(i).get("platform_id").toString()) != null) {
					maps.get(lists.get(i).get("platform_id").toString()).add(
							lists.get(i));
				} else if(maps.get(lists.get(i).get("platform_id").toString()) == null ||  maps.get(lists.get(i).get("platform_id").toString()).size()<=5){
					List<Object> listOne = new ArrayList<Object>();
					listOne.add(lists.get(i));
					maps.put(lists.get(i).get("platform_id").toString(),
							listOne);
				}
			}

		}
		maps.put("listNameDetile", listNameDetile);
		maps.put("listNameDetileOrId", listNameDetileOrId);
		return maps;
	}
	public  void platformOnclick(){
		if(getParaToInt("platformWho")==1){//如果等于1的时候加载平台数据
			String provinceId = getPara("provinceId");
			List<Data> data = Data.dao.getMaxDate();
			String time = data.get(0).getDate("data_date").toString();
			List<Data> dataList = dataService.getDataList(time + "/" + time,
					"volume desc", provinceId);
			setAttr("dataList", dataList);
			setAttr("time", time);
			renderJsp("platformContent.jsp");
		}else if (getParaToInt("platformWho")==2){//如果等于2的时候加载行业数据
			JDateTime jdt = new JDateTime();
			jdt.addMonth(-1);
			jdt.setDay(1);
			String startTime = jdt.toString("YYYY-MM-DD");
			jdt.addMonth(1);
			jdt.setDay(1);
			String endTime = jdt.toString("YYYY-MM-DD");
			List<ProvinceIndustry> provinceIndustryList = dataService
					.getProvinceIndustry(startTime, endTime, null);
			List<DateIndustry> dateIndustryList = dataService.getDateIndustry();
			setAttr("provinceIndustryList", provinceIndustryList);
			setAttr("dateIndustryList", dateIndustryList);
			setAttr("date", new JDateTime());
			renderJsp("industry.jsp");
		}else if (getParaToInt("platformWho")==3){//如果等于3的时候加载我的关注
			if(getSessionAttr(Global.SESSION_LOGIN_KEY)==null){
				renderJsp(Global.REDIRECT_URL);
			}else{
				String provinceId = getPara("provinceId");
				String timeStr = getPara("timeStr");
				if(StringUtils.isBlank(timeStr)){
					timeStr = DataUtil.createTypeStr(DataStoreType.YESTERDAY.getCode());
				}
				String uid = String.valueOf(getUserId());
				List<PlatformAttention> dataList = dataService1.getDataList(timeStr, "volume desc", provinceId,uid);
				List<Province> provinceList = dataService.getAllProvinces();
				setAttr("dataList", dataList);
				setAttr("provinceList", provinceList);
				renderJsp("my_concern.jsp");
			}
		}else{//如果等于3的时候加载问题平台
			List<Province> provinceList = dataService.getAllProvinces();
			setAttr("provinceList", provinceList);
			renderJsp("question.jsp");
		}
	}
	
	public void importData(){
		if(getSessionAttr(Global.SESSION_LOGIN_KEY) != null&&getSessionAttr(Global.SESSION_LOGIN_NAME_KEY).equals("曹亚鑫")){
			List<Data> data = Data.dao.getMaxDate();
			String time = data.get(0).getDate("data_date").toString();
			List<Data> pid = dataService.getPids(time);
			//List<String> week = EchartsUtils.getXzhou(data.get(0).getDate("data_date"), 2);
			//List<String> month = EchartsUtils.getXzhou(data.get(0).getDate("data_date"), 3);
			
			//for(Data d:pid){
			Long a = System.currentTimeMillis();
			for(int i=0;i<pid.size();i++){
				// 日数据处理
				//int id = d.getInt("platform_id");
				int id = pid.get(i).getInt("platform_id");
				List<Data> daydata = dataService.getDayData(new JDateTime(time).addDay(-11).toString("YYYY-MM-DD"), id);
				RedisUtil.set(id+"-"+"day", (Serializable)daydata);
				// 周数据
				List<Data> sumWeekData = dataService.getSumData(EchartsUtils.getXzhou(data.get(0).getDate("data_date"), 2), id);
				List<Data> commonWeekData = dataService.getCommonData(EchartsUtils.getXzhou(data.get(0).getDate("data_date"), 2), id);
				List<Data> avgWeekData = dataService.getAvgData(EchartsUtils.getXzhou(data.get(0).getDate("data_date"), 2), id);
				RedisUtil.set("sum"+"-"+id+"-"+"week", (Serializable)sumWeekData);
				RedisUtil.set("common"+"-"+id+"-"+"week", (Serializable)commonWeekData);
				RedisUtil.set("avg"+"-"+id+"-"+"week", (Serializable) avgWeekData);
				// 月数据
				List<Data> sumMonthData = dataService.getSumData(EchartsUtils.getXzhou(data.get(0).getDate("data_date"), 3), id);
				List<Data> commonMonthData = dataService.getCommonData(EchartsUtils.getXzhou(data.get(0).getDate("data_date"), 3), id);
				List<Data> avgMonthData = dataService.getAvgData(EchartsUtils.getXzhou(data.get(0).getDate("data_date"), 3), id);
				RedisUtil.set("sum"+"-"+id+"-"+"month", (Serializable)sumMonthData);
				RedisUtil.set("common"+"-"+id+"-"+"month", (Serializable)commonMonthData);
				RedisUtil.set("avg"+"-"+id+"-"+"month", (Serializable)avgMonthData);
			}
			
			System.out.println(a-System.currentTimeMillis());
		}else{
			redirect(Global.LOGIN_URL);
		}
	}
}
